package com.arsl.client.util;

import org.vaadin.gwtgraphics.client.shape.Rectangle;

import com.arsl.client.data.Content;
import com.arsl.client.view.ContentButton;
import com.arsl.client.view.ContentImageButton;
import com.arsl.client.view.ContentInputWidget;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.ButtonBase;

public class Utils {

	private static final char[] CHARS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
			.toCharArray();

	public static boolean isNullOrEmpty(final String string) {
		return string == null || "".equals(string.trim());
	}

	public static float[] getresizedDimensions(float height, float width,
			float defaultWidth) {
		float[] newDimensions = new float[2];
		newDimensions[0] = defaultWidth;
		newDimensions[1] = defaultWidth * height / width;
		return newDimensions;
	}

	public static ContentButton getButton(String text,
			ClickHandler eventHandler, String width, String height,
			String styleName, String primaryStyleName, ContentInputWidget widget) {
		ContentButton btn = new ContentButton();
		if (widget != null)
			btn.setParentWidget(widget);
		setCommonButtonProperties(btn, text, eventHandler, width, height,
				styleName, primaryStyleName, widget);

		return btn;
	}

	public static ContentImageButton getImageButton(String text,
			ClickHandler eventHandler, String width, String height,
			String styleName, String primaryStyleName,
			ContentInputWidget widget, String imageUrl) {

		ContentImageButton btn;
		if (!Utils.isNullOrEmpty(imageUrl)) {
			btn = new ContentImageButton(imageUrl);
		} else {
			btn = new ContentImageButton();
		}

		if (widget != null)
			btn.setParentWidget(widget);

		setCommonButtonProperties(btn, text, eventHandler, width, height,
				styleName, primaryStyleName, widget);

		return btn;
	}

	public static String randomUUID() {
		char[] uuid = new char[36];
		int r;
		uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-';
		uuid[14] = '4';
		for (int i = 0; i < 36; i++)
			if (uuid[i] == 0) {
				r = (int) (Math.random() * 16);
				uuid[i] = CHARS[(i == 19) ? (r & 0x3) | 0x8 : r & 0xf];
			}
		return new String(uuid);
	}

	public static boolean rectContainsPoint(Rectangle rect, int x, int y) {
		return rect.getX() <= x && x <= rect.getX() + rect.getWidth()
				&& rect.getY() <= y && y <= rect.getY() + rect.getHeight();
	}

	private static void setCommonButtonProperties(ButtonBase btn, String text,
			ClickHandler eventHandler, String width, String height,
			String styleName, String primaryStyleName, ContentInputWidget widget) {
		if (!Utils.isNullOrEmpty(text))
			btn.setText(text);

		if (!Utils.isNullOrEmpty(width))
			btn.setWidth(width);

		if (!Utils.isNullOrEmpty(height))
			btn.setHeight(height);

		if (!Utils.isNullOrEmpty(primaryStyleName))
			btn.setStylePrimaryName(primaryStyleName);

		if (!Utils.isNullOrEmpty(styleName))
			btn.addStyleName(styleName);

		if (eventHandler != null)
			btn.addClickHandler(eventHandler);

	}

	public static boolean isValidContent(Content content) {
		if (Constants.CALL_CONTENT_TYPE.equalsIgnoreCase(content.getType())
				|| Constants.SMS_CONTENT_TYPE.equalsIgnoreCase(content
						.getType())) {
			return !(Utils.isNullOrEmpty(content.getTitle()) || Utils
					.isNullOrEmpty(content.getDescription()));
		}
		if (Utils.isNullOrEmpty(content.getTitle())
				|| Utils.isNullOrEmpty(content.getType())
				|| Utils.isNullOrEmpty(content.getUrl())) {
			return false;
		}
		return true;
	}
}
